/*
MIT License

Copyright (c) 2022 chen-chaochen,guo-yichen@Easylosu

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 
SOFTWARE.

*/

#include<iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <vector>
#include <string>
#include <sys/socket.h>
#include <sys/unistd.h>
#include <sys/types.h>
#include <sys/errno.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include "els_c_api.h"
using namespace std;
extern "C"{
LNI_return losu_socket_freenum(LNI_argv a);

LNI_return losu_socket_Sopen(LNI_argv a);
LNI_return losu_socket_Ssend(LNI_argv a);
LNI_return losu_socket_Sread(LNI_argv a);
LNI_return losu_socket_Sclose(LNI_argv a);

LNI_return losu_socket_Copen(LNI_argv a);
LNI_return losu_socket_Csend(LNI_argv a);
LNI_return losu_socket_Cread(LNI_argv a);
LNI_return losu_socket_Cclose(LNI_argv a);

#define MAXSOCKET 128
#define MAXLINK 2048
#define BUFFSIZE 2048
string rtstr;
LNI_return rt;
int sockfd[MAXSOCKET],socknd[MAXSOCKET];
struct sockaddr_in servaddr[MAXSOCKET]; 
char buff[BUFFSIZE]; 
char tmp[BUFFSIZE];
int bool_ptr[MAXSOCKET];

LNI_return losu_socket_freenum(LNI_argv a){
    try{
        rt.type = ELS_TYPE_NUMBER;
        for(int i=0;i<MAXSOCKET;i++){
            if(bool_ptr[i]==0) {
                rt.num = i;
                return rt;
            }
        }
        throw "套接池溢出\t没有空余的套接字!";
    }catch(const char* ei){
        //cout<<ei<<endl;
        exit(1);
    }
}

LNI_return losu_socket_Sopen(LNI_argv a){
    try{
        rt.type = ELS_TYPE_NUMBER;
        int PTR = a.num[3];
        int PORT = a.num[4];
        sockfd[PTR] = socket(AF_INET, SOCK_STREAM, 0); 
        if (sockfd[PTR]==-1) 
            throw "套接字生成错误";

        bzero(&servaddr[PTR], sizeof(servaddr[PTR]));
        servaddr[PTR].sin_family = AF_INET;
        servaddr[PTR].sin_addr.s_addr = htonl(INADDR_ANY);
        servaddr[PTR].sin_port = htons(PORT);

        if (bind(sockfd[PTR], (struct sockaddr*)&servaddr[PTR], sizeof(servaddr[PTR]))==-1)
            throw "套接字生成错误";
        if (listen(sockfd[PTR], MAXLINK)==-1)
            throw "套接字生成错误";

        bool_ptr[PTR]=1;
        rt.num = 1;
        socknd[PTR] = socknd[PTR];
        return rt;

    }catch(const char* ei){
        //cout<<ei<<endl;
        rt.num=0;
        return rt;
    }
    
}
LNI_return losu_socket_Ssend(LNI_argv a){
    try{    
        rt.type = ELS_TYPE_NULL;
        int PTR = a.num[3];
        if(a.type[4]==ELS_TYPE_STRING){
            sprintf(tmp,"%s",a.str[4]);
        }else if(a.type[4]==ELS_TYPE_NUMBER){
            gcvt(a.num[4],7,tmp);
        }
        
        send(socknd[PTR], tmp, strlen(tmp), 0);
        return rt;
    }catch(const char* ei){
        //cout<<ei<<endl;
        exit(1);
    }
}
LNI_return losu_socket_Sread(LNI_argv a){
    try{    
        rt.type = ELS_TYPE_STRING;
        int PTR = a.num[3];
        socknd[PTR] = accept(sockfd[PTR], NULL, NULL);
        if (-1 == socknd[PTR])
            throw "套接字异常";
        bzero(buff, BUFFSIZE);
        recv(socknd[PTR], buff, BUFFSIZE - 1, 0);
        rtstr = buff;
        rt.str = (char*)rtstr.c_str();
        return rt;
    }catch(const char* ei){
        //cout<<ei<<endl;
        exit(1);
    }
}
LNI_return losu_socket_Sclose(LNI_argv a){
    rt.type = ELS_TYPE_NULL;
    int PTR = a.num[3];
    close(sockfd[PTR]);
    bool_ptr[PTR]=0;
    return rt;
}

LNI_return losu_socket_Copen(LNI_argv a){
    try{
        rt.type = ELS_TYPE_NUMBER;
        int PTR = a.num[3];
        int PORT = a.num[5];
        char IP[128];
        sprintf(IP,"%s",a.str[4]);
        sockfd[PTR] = socket(AF_INET, SOCK_STREAM, 0); 
        
        if (sockfd[PTR]==-1) 
            throw "套接字生成错误: 无法连接到指定地址";
        bzero(&servaddr[PTR], sizeof(servaddr[PTR]));
        servaddr[PTR].sin_family = AF_INET;
        inet_pton(AF_INET, IP, &servaddr[PTR].sin_addr);
        servaddr[PTR].sin_port = htons(PORT);
        if(connect(sockfd[PTR], (struct sockaddr*)&servaddr[PTR], sizeof(servaddr[PTR]))==-1)
            throw "套接字生成错误: 绑定失败";
        bool_ptr[PTR]=1;
        rt.num=1;
        socknd[PTR] = socknd[PTR];
        return rt;

    }catch(const char* ei){
        //cout<<ei<<endl;
        rt.num=0;
        return rt;
    }
    
}
LNI_return losu_socket_Csend(LNI_argv a){
    try{    
        rt.type = ELS_TYPE_NULL;
        int PTR = a.num[3];
        if(a.type[4]==ELS_TYPE_STRING){
            sprintf(tmp,"%s",a.str[4]);
        }else if(a.type[4]==ELS_TYPE_NUMBER){
            gcvt(a.num[4],7,tmp);
        }
        send(sockfd[PTR], tmp, strlen(tmp), 0);
        
        return rt;
    }catch(const char* ei){
        //cout<<ei<<endl;
        exit(1);
    }
}
LNI_return losu_socket_Cread(LNI_argv a){
    try{    
        rt.type = ELS_TYPE_STRING;
        int PTR = a.num[3];
        
        bzero(buff, BUFFSIZE);
        recv(sockfd[PTR], buff, BUFFSIZE - 1, 0);
        rtstr = buff;
        rt.str = (char*)rtstr.c_str();
        return rt;
    }catch(const char* ei){
        rt.type = ELS_TYPE_NULL;
        //cout<<ei<<endl;
        return rt;
    }
}
LNI_return losu_socket_Cclose(LNI_argv a){
    rt.type = ELS_TYPE_NULL;
    int PTR = a.num[3];
    close(sockfd[PTR]);
    bool_ptr[PTR]=0;
    return rt;
}

}